﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class LookupRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<Lookup> GetAllData()
        {
            return from Lookup in db.Lookups
                   orderby Lookup.TableName descending
                   select Lookup;
        }

        public IQueryable<Lookup> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.Lookups.Where(p => p.TableName.Contains(searchString));
            if (searchField == "TableName")
                ps = db.Lookups.Where(p => p.TableName.Contains(searchString));
            if (searchField == "ColumnName")
                ps = db.Lookups.Where(p => p.ColumnName.Contains(searchString));
            if (searchField == "LookupValue")
                ps = db.Lookups.Where(p => p.LookupValue.Contains(searchString));
            if (searchField == "LookupText")
                ps = db.Lookups.Where(p => p.LookupText.Contains(searchString));
            if (searchField == "All")
                ps = db.Lookups.Where(p => p.TableName.Contains(searchString)
                                        || p.ColumnName.Contains(searchString) 
                                        || p.LookupValue.Contains(searchString)
                                        || p.LookupText.Contains(searchString));

            return ps.AsQueryable();

        }

        public IQueryable<Lookup> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }




        public Lookup GetByID(string id)
        {
            return db.Lookups.SingleOrDefault(Lookup => Lookup.TableName == id);
        }

        //Inset
        public void Add(Lookup Lookup)
        {
            //if (Lookup.StatusID == 0 )
            //    Lookup.StatusID = 1;
            Lookup.InsertedUser = HttpContext.Current.User.Identity.Name;
            Lookup.InsertedDate = DateTime.Now;
            Lookup.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Lookup.UpdatedDate = DateTime.Now;
            Lookup.DataVersion = 1;

            db.Lookups.AddObject(Lookup);
        }

        //Update
        public void Update(Lookup Lookup)
        {

            Lookup originalLookup = GetByID(Lookup.TableName);
            
            Lookup.InsertedUser = originalLookup.InsertedUser;
            Lookup.InsertedDate = originalLookup.InsertedDate;
            Lookup.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Lookup.UpdatedDate = DateTime.Now;
            Lookup.DataVersion = ++originalLookup.DataVersion;
  
            db.Lookups.ApplyCurrentValues(Lookup);
        }

        //Delete
        public void Delete(Lookup Lookup)
        {
            db.Lookups.DeleteObject(Lookup);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}